<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="bug来源" prop="bugSource">
						<el-select v-model="form.bugSource" placeholder="请选择bug来源">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in bug_source"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="主任务" prop="taskId" v-if="form.bugSource == '1' || form.bugSource == ''">
						<el-select
							:disabled="form.bugSource == ''"
							v-model="form.taskId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入主任务名称"
							:remote-method="getMainTaskList"
							style="width: 240px"
							@change="selectMainTaskChange"
						>
							<el-option v-for="item in mainTaskList" :key="item.id" :label="item.taskName" :value="item.id" />
						</el-select>
					</el-form-item>
					<el-form-item label="支线任务" prop="taskId" v-if="form.bugSource == '2'">
						<el-select
							v-model="form.taskId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入支线任务名称"
							:remote-method="getSubTaskList"
							style="width: 240px"
							@change="selectSubTaskChange"
						>
							<el-option v-for="item in subTaskList" :key="item.id" :label="item.taskName" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="bug名称" prop="bugName">
						<el-input v-model="form.bugName" placeholder="请输入bug名称" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="bug类型" prop="bugType">
						<el-select v-model="form.bugType" placeholder="请选择bug类型">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in bug_type"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="bug状态" prop="bugStatus">
						<el-select v-model="form.bugStatus" placeholder="请选择bug状态">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in task_status"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="负责人" prop="commander">
						<UserSelect placeholder="负责人" v-model="form.commander" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="bug描述" prop="bugDesc">
						<el-input type="textarea" v-model="form.bugDesc" placeholder="请输入bug描述" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="bug内容" prop="bugContent">
						<Editor v-model:getHtml="form.bugContent" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ProjectTaskBugDialog">
import { useDict } from '@/hooks/dict';
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/projectTaskBug';
import { rule } from '@/utils/validate';
import UserSelect from '@/components/UserSelect/index.vue';
import { fetchList as fetchMainTaskList } from '@/api/oa/projectMainTask';
import { fetchList as fetchSubTaskList } from '@/api/oa/projectSubTask';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const mainTaskList = ref();
const subTaskList = ref();
// 定义字典
const { task_status, bug_type, bug_source } = useDict('task_status', 'bug_type', 'bug_source');

function generateForm() {
	return {
		id: '',
		taskId: '',
		bugType: '',
		bugSource: '',
		bugName: '',
		bugDesc: '',
		bugContent: '',
		bugStatus: '',
		commander: '',
	};
}

// 提交表单数据
const form = ref(generateForm());

// 定义校验规则
const dataRules = ref({
	taskId: [{ required: true, message: '任务ID不能为空', trigger: 'blur' }],
	bugType: [{ required: true, message: 'bug类型不能为空', trigger: 'blur' }],
	bugSource: [{ required: true, message: 'bug来源不能为空', trigger: 'blur' }],
	bugName: [{ required: true, message: 'bug名称不能为空', trigger: 'blur' }],
	bugDesc: [{ required: true, message: 'bug描述不能为空', trigger: 'blur' }],
	bugContent: [{ required: true, message: 'bug内容不能为空', trigger: 'blur' }],
	bugStatus: [{ required: true, message: 'bug状态不能为空', trigger: 'blur' }],
	commander: [{ required: true, message: '负责人ID不能为空', trigger: 'blur' }],
});
const getMainTaskList = async (p: string) => {
	p = p.toUpperCase();
	let params = {
		size: 200,
		taskName: p,
		taskStatus: 'closed',
	};
	fetchMainTaskList(params)
		.then((res: any) => {
			mainTaskList.value = res.data.records;
		})
		.finally(() => {});
};
const selectMainTaskChange = async (e: any) => {
	form.value.taskName = mainTaskList.value.find((item: any) => item.taskName == e).taskName;
};
const getSubTaskList = async (p: string) => {
	p = p.toUpperCase();
	let params = {
		size: 200,
		taskName: p,
		taskStatus: 'closed',
	};
	fetchSubTaskList(params)
		.then((res: any) => {
			subTaskList.value = res.data.records;
		})
		.finally(() => {});
};
const selectSubTaskChange = async (e: any) => {
	form.value.taskName = mainTaskList.value.find((item: any) => item.taskName == e).taskName;
};

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.value.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取projectTaskBug信息
	if (id) {
		form.value.id = id;
		getprojectTaskBugData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.value.id ? await putObj(form.value) : await addObj(form.value);
		useMessage().success(form.value.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getprojectTaskBugData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form.value, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
